package com.kingland.zf.service;

import com.kingland.zf.mapper.UserModelMapper;
import com.kingland.zf.model.UserModel;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import java.util.Collection;

public class UserDetailServiceImpl implements UserDetailsService {
    private  final UserModelMapper userModelMapper;

    public UserDetailServiceImpl(UserModelMapper userModelMapper){
        this.userModelMapper = userModelMapper;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserModel userModel = this.userModelMapper.selectUsername(username);
        return new UserDetails() {
            @Override
            public Collection<? extends GrantedAuthority> getAuthorities() {
                return null;
            }

            @Override
            public String getPassword() {
                return userModel.getPassword();
            }

            @Override
            public String getUsername() {
                return userModel.getUsername();
            }

            @Override
            public boolean isAccountNonExpired() {
                return true;
            }

            @Override
            public boolean isAccountNonLocked() {
                return true;
            }

            @Override
            public boolean isCredentialsNonExpired() {
                return true;
            }

            @Override
            public boolean isEnabled() {
                return true;
            }
        };
    }
}
